import { constantRoutes, asyncRoutes } from '@/router'

const state = {
  routes: []
}
const mutations = {
  setRoutes(state, routes) {
    // 封装一个 mutations 专门讲静态路由，和有权限的动态路由合并
    // 交给 state 储存
    // 用来渲染菜单，而不是原来的所有菜单都显示出来
    state.routes = [...constantRoutes, ...routes]
  }
}
const actions = {
  filterRoutes(store, menus) {
    const routes = asyncRoutes.filter(route => menus.indexOf(route.name) > -1)
    // 解决菜单问题，将筛选过后的动态路由交给 mutations
    store.commit('setRoutes', routes)
    // 除了上面用 mutations 设置菜单以外
    // 这里还将筛选后的数据返回出去
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
